from qt_core import *

style = """
/* 主控件样式 */
QComboBox {{
    background-color: {_bg_color};
    border-radius: {_radius}px;
    border: {_border_size}px solid transparent;
    padding-left: 10px;
    padding-right: 25px;
    color: {_color};
    height: 30px;
    min-width: 100px;
}}
QComboBox:focus {{
    border: {_border_size}px solid {_context_color};
    background-color: {_bg_color_active};
}}
QComboBox:hover {{
    border: {_border_size}px solid {_context_color};
}}

/* 下拉选项框样式 */
QComboBox QAbstractItemView {{
    /* 容器样式 */
    border: {_border_size}px solid {_context_color};
    border-radius: {_radius}px;
    background: {_bg_color};
    margin-top: {_border_size}px;  /* 与控件的间距 */
    outline: 0;  /* 移除选中虚线框 */
}}

/* 单个选项项样式 */
QComboBox QAbstractItemView::item {{
    height: 30px;
    padding: 0 10px;
    color: {_color};
    border-radius: {_item_radius}px;
    background-color: {_bg_color};
}}
QComboBox QAbstractItemView::item:hover {{
    background-color: {_context_color};
    color: {_selection_color};
    border-radius: {_item_radius}px;
}}
QComboBox QAbstractItemView::item:selected {{
    background-color: {_context_color};
    color: {_selection_color};
    border-radius: {_item_radius}px;
}}
QComboBox::drop-down {{
    subcontrol-origin: padding;
    subcontrol-position: right center;
    width: 20px;
    border: none;
}}
QComboBox::down-arrow {{
    image: url(res/SYS/images/svg_icons/icon_arrow_right.svg);
    width: 16px;
    height: 16px;
}}
QComboBox::down-arrow:on {{
    image: url(res/SYS/images/svg_icons/icon_arrow_down.svg);
    width: 16px;
    height: 16px;
}}

QComboBox QAbstractScrollArea QScrollBar{{
    border-radius:6px;
    width: 10px;
    background:transparent;
}}


"""


class PyComboBox(QComboBox):
    def __init__(
        self,
        ColorPalette,
        place_holder_text="",
        radius=8,
        border_size=2,
        item_radius=4,  # 新增选项圆角参数
    ):
        super().__init__()
        # 设置占位符文本
        self.setEditable(True)
        self.lineEdit().setPlaceholderText(place_holder_text)
        self.lineEdit().setReadOnly(True)
        self.setEditable(False)
        self.view().window().setWindowFlags(
            Qt.Popup | Qt.FramelessWindowHint | Qt.NoDropShadowWindowHint
        )
        self.view().window().setAttribute(Qt.WA_TranslucentBackground)
        # 设置样式
        self.set_stylesheet(
            radius,
            border_size,
            ColorPalette.text_foreground,
            ColorPalette.text_foreground,
            ColorPalette.bg_three,
            ColorPalette.bg_three,
            ColorPalette.text_description,
            item_radius,  # 传递新参数
        )

        # 禁用虚线焦点框
        self.setFocusPolicy(Qt.StrongFocus)

    def set_stylesheet(
        self,
        radius,
        border_size,
        color,
        selection_color,
        bg_color,
        bg_color_active,
        context_color,
        item_radius,
    ):
        style_format = style.format(
            _radius=radius,
            _border_size=border_size,
            _color=color,
            _selection_color=selection_color,
            _bg_color=bg_color,
            _bg_color_active=bg_color_active,
            _context_color=context_color,
            _item_radius=item_radius,  # 新增参数应用
        )
        self.setStyleSheet(style_format)

    def focusOutEvent(self, event):
        super().focusOutEvent(event)
        self.style().unpolish(self)
        self.style().polish(self)
